package com.hk.controller;

import com.hankcs.hanlp.HanLP;
import com.hankcs.hanlp.seg.common.Term;
import com.hk.entity.Result;
import com.hk.entity.StudentSkills;
import com.hk.service.StudentSkillsService;
import com.hk.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 学生技能信息表控制器
 */

@Slf4j
@RestController
@RequestMapping("/StudentSkills")
public class StudentSkillsController {
    @Autowired
    private StudentSkillsService studentSkillsService;

    /**
     * 更新语言技能
     * @param languageSkills
     * @param authorization
     * @return
     */
    @PutMapping("/language")
    public Result updatalanguageSkills(@RequestParam("languageSkills") String languageSkills, @RequestHeader("token") String authorization){
        // 从token头中提取JWT令牌
        String token = authorization;

        // 解析JWT令牌并提取管理员ID
        Map<String, Object> claims = JwtUtils.parseJWT(token);
        String stuId = (String) claims.get("stuId");
        log.info("languageSkills: {}",languageSkills);
        studentSkillsService.UUpdatalanguageSkills(languageSkills,stuId);
        return Result.success();
    }

    /**
     * 查询语言技能信息
     * @param authorization
     * @return
     */
    @GetMapping("/languageInfo")
    public Result languageSkillsInfo(@RequestHeader("token") String authorization){
        // 从token头中提取JWT令牌
        String token = authorization;

        // 解析JWT令牌并提取管理员ID
        Map<String, Object> claims = JwtUtils.parseJWT(token);
        String stuId = (String) claims.get("stuId");
        StudentSkills studentSkills = studentSkillsService.getlanguageInfo(stuId);
        return Result.success(studentSkills);
    }

    /**
     * 更新体质技能
     * @param physicalSkills
     * @param authorization
     * @return
     */
    @PutMapping("/physical")
    public Result updataphysicalSkills(@RequestParam("physicalSkills") String physicalSkills, @RequestHeader("token") String authorization){
        // 从token头中提取JWT令牌
        String token = authorization;

        // 解析JWT令牌并提取管理员ID
        Map<String, Object> claims = JwtUtils.parseJWT(token);
        String stuId = (String) claims.get("stuId");
        log.info("languageSkills: {}",physicalSkills);
        studentSkillsService.UpdataphysicalSkills(physicalSkills,stuId);
        return Result.success();
    }

    /**
     * 查询体质技能信息
     * @param authorization
     * @return
     */
    @GetMapping("/physicalInfo")
    public Result physicalSkillsInfo(@RequestHeader("token") String authorization){
        // 从token头中提取JWT令牌
        String token = authorization;

        // 解析JWT令牌并提取管理员ID
        Map<String, Object> claims = JwtUtils.parseJWT(token);
        String stuId = (String) claims.get("stuId");
        StudentSkills studentSkills = studentSkillsService.getphysicalInfo(stuId);
        return Result.success(studentSkills);
    }

    /**
     * 更新附加技能
     * @param additionalSkills
     * @param authorization
     * @return
     */
    @PutMapping("/additional")
    public Result updataadditionalSkills(@RequestParam("additionalSkills") String additionalSkills, @RequestHeader("token") String authorization){
        // 从token头中提取JWT令牌
        String token = authorization;

        // 解析JWT令牌并提取管理员ID
        Map<String, Object> claims = JwtUtils.parseJWT(token);
        String stuId = (String) claims.get("stuId");
        log.info("languageSkills: {}",additionalSkills);
        studentSkillsService.UpdataadditionalSkills(additionalSkills,stuId);
        return Result.success();
    }

    /**
     * 查询附加技能信息
     * @param authorization
     * @return
     */
    @GetMapping("/additionalInfo")
    public Result additionalInfo(@RequestHeader("token") String authorization){
        // 从token头中提取JWT令牌
        String token = authorization;

        // 解析JWT令牌并提取管理员ID
        Map<String, Object> claims = JwtUtils.parseJWT(token);
        String stuId = (String) claims.get("stuId");
        StudentSkills studentSkills = studentSkillsService.getadditionalInfo(stuId);
        return Result.success(studentSkills);
    }

    /**
     *  更新专业技能
     * @param professionalSkills
     * @param authorization
     * @return
     */
    @PutMapping("/professional")
    public Result updataprofessionalSkills(@RequestParam("professionalSkills") String professionalSkills, @RequestHeader("token") String authorization){
        // 从token头中提取JWT令牌
        String token = authorization;

        // 解析JWT令牌并提取管理员ID
        Map<String, Object> claims = JwtUtils.parseJWT(token);
        String stuId = (String) claims.get("stuId");
        log.info("languageSkills: {}",professionalSkills);
        studentSkillsService.UpdataprofessionalSkills(professionalSkills,stuId);
        return Result.success();
    }

    /**
     * 查询专业技能信息
     * @param authorization
     * @return
     */
    @GetMapping("/professionalInfo")
    public Result professionalInfo(@RequestHeader("token") String authorization){
        // 从token头中提取JWT令牌
        String token = authorization;

        // 解析JWT令牌并提取管理员ID
        Map<String, Object> claims = JwtUtils.parseJWT(token);
        String stuId = (String) claims.get("stuId");
        StudentSkills studentSkills = studentSkillsService.getprofessionalInfo(stuId);
        return Result.success(studentSkills);
    }

    @GetMapping("/allInfo")
    public Result allInfo(@RequestHeader("token") String authorization){
        // 从token头中提取JWT令牌
        String token = authorization;

        // 解析JWT令牌并提取管理员ID
        Map<String, Object> claims = JwtUtils.parseJWT(token);
        String stuId = (String) claims.get("stuId");
        StudentSkills studentSkills = studentSkillsService.allInfo(stuId);
        // 将技能信息转换为词云数据
        List<Map<String, Object>> wordCloudData = generateWordCloudData(studentSkills);

        // 返回原始学生技能信息和词云数据
        Map<String, Object> responseData = new HashMap<>();
//        responseData.put("studentSkills", studentSkills);
        responseData.put("wordCloudData", wordCloudData);

        return Result.success(responseData);
    }
    /**
     * 根据学生技能信息生成词云数据
     *
     * @param studentSkills 学生技能信息
     * @return 词云数据
     */
    private List<Map<String, Object>> generateWordCloudData(StudentSkills studentSkills) {
        // 合并所有技能描述
        String allText = String.join(" ",
                studentSkills.getLanguageSkills(),
                studentSkills.getPhysicalSkills(),
                studentSkills.getAdditionalSkills(),
                studentSkills.getProfessionalSkills()
        );

        // 使用 HanLP 分词
        List<Term> terms = HanLP.segment(allText);

        // 统计词频
        Map<String, Long> wordFrequency = terms.stream()
                .filter(term -> term.word.length() > 1) // 过滤掉长度为1的无意义词
                .collect(Collectors.groupingBy(term -> term.word, Collectors.counting()));

        // 转换为前端需要的格式
        return wordFrequency.entrySet().stream()
                .map(entry -> {
                    Map<String, Object> wordMap = new HashMap<>();
                    wordMap.put("word", entry.getKey());
                    wordMap.put("weight", entry.getValue());
                    return wordMap;
                })
                .sorted((a, b) -> Long.compare((Long) b.get("weight"), (Long) a.get("weight"))) // 按权重降序排序
                .collect(Collectors.toList());
    }


}
